#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import xml.dom.minidom
from ConfigParser import ConfigParser

__RPATH = ""
__LanguageSet = ""
__XmlPath=""
__DicResources={} # 资源字典{key="语言编码",text="文本"}
__DicLanguage={} # 语言字典{key="语言"，text={langValue,langText}}
__DicCountry={}

class xmlCfObj:
    def __init__(self):
        self.t=""
        self.v=""

def getText(lang,strCode,*strReplaces):#根据语言，文本编码获取相对应文本值
    global __DicResources
    if len(__DicResources)<1:
        return strCode
    _text=__DicResources.get(lang+strCode)
    if _text==None:
        _text=__DicResources.get("en-us"+strCode)
        if _text==None:
            return strCode
    _i=0
    for s in strReplaces:
        _text=_text.replace("{%d}" %_i,s)
        _i+=1
    return _text

def getLangText(lang,value):#XML语言值
    global __DicLanguage
    if value==None or value.strip()=="":
        return ""
    value=value.strip().lower()
    _list=__DicLanguage.get(lang)
    if _list==None:
        return ""
    for obj in _list:
        if obj.v==value:
            return obj.t
    return ""

def getCountryText(lang,value):#XML国家值
    global __DicCountry
    if value==None or value.strip()=="":
        return ""
    value=value.strip().lower()
    _list=__DicCountry.get(lang)
    if _list==None:
        return ""
    for obj in _list:
        if obj.v==value:
            return obj.t
    return ""

def loadResFiles(appConfig):# 加载资源文件
    global __DicResources
    _tem_dic={}
    _rP = appConfig.get("AppSettings","RPath")
    for path,subdirs,files in os.walk(_rP):
        _txtObj={}
        for _f in files:
            _sp_f=_f.split('.')
            if len(_sp_f)==3 and _sp_f[2]=="txt":
                _content_lines=file(os.path.join(path, _f)).read().splitlines()
                for _line in _content_lines:
                    if (_line.strip()=="" or _line[0:2]=="//" or "=" not in _line):
                        continue
                    _sp_line=_line.split("=",1)
                    _key=_sp_f[1].lower()+_sp_line[0].strip()
                    if not _tem_dic.has_key(_key):
                        _tem_dic[_key]=_sp_line[1].strip()
    if len(__DicResources)>0:
        __DicResources.clear()
    __DicResources=_tem_dic

# """根据指定XML文件名(不带后缀名)解析XML配置路径下所有语言文件，返回各个语言版本内容{语言,[{v=Value,t=Text}]}"""
def getAllLangs_Cf_XML_VT_List(appConfig,fname):
    _dicObj={}
    _spLSet=appConfig.get("AppSettings","LanguageSet").split('$')
    for lan in _spLSet:
        _xmlPath="%s/%s/%s.xml" %(appConfig.get("AppSettings","XmlPath"),lan,fname)            
        if not os.path.isfile(_xmlPath):
            continue
        xmlDom=xml.dom.minidom.parse(_xmlPath)            
        items=xmlDom.documentElement.getElementsByTagName("Item")
        _list=[]
        for item in items:
            _o=xmlCfObj()
            if item.getElementsByTagName('Value')[0].childNodes.length>0:
                _o.v=item.getElementsByTagName('Value')[0].childNodes[0].nodeValue.lower()
            if item.getElementsByTagName('Text')[0].childNodes.length>0:
                _o.t=item.getElementsByTagName('Text')[0].childNodes[0].nodeValue
            _list.append(_o)
        if len(_list)>0:
            _dicObj[lan.lower()]=_list            
    return _dicObj

def loadLanguageXml(appConfig):# 加载语言XML文件
    global __DicLanguage
    __DicLanguage=getAllLangs_Cf_XML_VT_List(appConfig,"Languages")

def loadCountryXml(appConfig):# 加载国家XML文件
    global __DicCountry
    __DicCountry=getAllLangs_Cf_XML_VT_List(appConfig,"Country") 

def __init__():
    __AppConfig = ConfigParser()
    __AppConfig.read("/home/longxin/work/italki/italki.conf")
    __AppConfig.get("AppSettings","LanguageSet")
    loadResFiles(__AppConfig)
    loadLanguageXml(__AppConfig)
    loadCountryXml(__AppConfig)
    
    
    
